package com.hh.vietnameseproject.ui.me.record

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.hh.vietnameseproject.databinding.ActivityRecordBinding
import com.hh.vietnameseproject.logic.model.ReadList
import com.hh.vietnameseproject.logic.model.RecordList
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.read.ReadItemAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.util.ArrayList

class RecordActivity : AppCompatActivity() {
    lateinit var binding:ActivityRecordBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRecordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.recordRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)

        GlobalScope.launch {
            val body = AllNetWork.getRecord().string()
            val jsonObject = JSONObject(body)
            val array = jsonObject.getJSONArray("data")
            withContext(Dispatchers.Main){
                if(jsonObject.getInt("code")==1){
                    val list = ArrayList<RecordList>()
                    for(i in 0 until array.length()){
                        val item = array.getJSONObject(i)
                        list.add(RecordList(item.getString("date"),item.getString("continuousdays")))

                    }
                    binding.recordRecyclerView.adapter = RecordAdapter(this@RecordActivity,list)
                }
            }
        }
        binding.recordBack.setOnClickListener { onBackPressed() }
    }
}